package parser

import (
	"fmt"
	"regexp"
	"spider/crawler/engine"
)

//城市列表解析器

const cityList = `<a href="(http://www.zhenai.com/zhenghun/[0-9a-z]+)"[^>]*>([^<]+)</a>`

func ParseCityList(contents []byte) engine.ParseResult {
	// 正则
	re := regexp.MustCompile(cityList)
	matches := re.FindAllSubmatch(contents, -1)
	//定义一个变量
	result := engine.ParseResult{}
	limit := 10
	for _, m := range matches {
		result.Items = append(result.Items, "City"+string(m[2])) // 城市的名字
		result.Requests = append(result.Requests, engine.Request{
			Url: string(m[1]), //城市的url
			// ParserFunc: engine.NilParser,
			ParserFunc: ParseCity,
		})

		limit--
		if limit == 0 {
			break
		}

		fmt.Printf("City:%s,URL:%s\n", m[2], m[1])
	}
	return result
}
